KISSY.use(["node",'bui/tab'],function(S,Node,Tab){ 
        var tab = new Tab.TabPanel({
          srcNode : '#tab',
          elCls : 'nav-tabs',
          itemStatusCls : {
            'selected' : 'active'
          },
          panelContainer : '#panelcontent'//如果不指定容器的父元素，会自动生成
          //selectedEvent : 'mouseenter',//默认为click,可以更改事件
        });
        tab.render();
	$(".helper").on("click",function(){
		showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		$(".helper-in").on("click",function(){
			closeDialog();
			showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		});
	});
$(".oldcomer").on("click",function(){
	$(".jy_tms").val(1);
});
$(".jy_tms").on("click",function(){
	$(".chkjy").fire("click");
});
$(".newcomer").on("click",function(){
	$(".jy_tms").val(0);
});
$(".overamont").on("click",function(){
	if($(this).getDOMNode().checked==false){
		$(".order_pay_over_amount").val($(".order_pay_over_amount").attr("data-val"));
		$(".order_pay_over_amount").removeAttr("readonly");
	}else{
		$(".order_pay_over_amount").val("--");
		$(".order_pay_over_amount").attr("readonly","readonly");
	}
});
$(".brandme").on("click",function(){
	if($(this).getDOMNode().checked==true){
		$(".brandid").val($(".brandid").attr("data-val"));
		$(".brandid").removeAttr("readonly");
		$(".brandid").css("backgroundColor","#FFF");
	}else{
		$(".brandid").val(0);
		$(".brandid").attr("readonly","readonly");
		$(".brandid").css("backgroundColor","#DDD");
	}
});
$(".orderset").on("change",function(){
	var doms = ".order_payment,.order_pay_times,.order_success_times,.order_pay_over_amount,.goodrate_times,.goodrate_kwd,.pay-start-calendar,.pay-end-calendar ";
	if($(this).val()==0){
		$(".overamont").removeAttr("checked");
		// $(doms).val(0);
		$(doms).attr("readonly","readonly");
		$(doms).css("backgroundColor","#DDD");
	}else{
		// $(doms).val($(this).attr("data-val"));
		$(doms).removeAttr("readonly");
		$(doms).css("backgroundColor","#FFF");
	}
});
$(".onoff").on("change",function(){
	var doms = "."+$(this).attr("data-input");
	if($(this).val()==0){
		$(doms).attr("readonly","readonly");
		$(doms).css("backgroundColor","#DDD");
	}else{
		$(doms).removeAttr("readonly");
		$(doms).css("backgroundColor","#FFF");
	}
});
var i=0;
$(".js-duihuan,.js-jiacheng,.js-qddh,.js-dingdanjifen").on("click",function(){
	var id = $(this).attr("data-addid");
	var target = $(this).attr("data-target");
	if(id=="add"){
		var div = $(".div-"+target).clone(true,true,true);
		div.removeClass(".div-"+target);
		div.children(".controls").children(".js-"+target).attr("data-addid",i);
		div.children(".controls").children(".js-"+target).removeClass("button-success");
		div.children(".controls").children(".js-"+target).addClass("button-danger");
		div.children(".controls").children(".js-"+target).children("span").text("删除");
		div.children(".controls").children(".js-"+target).children(".icon").removeClass("icon-plus");
		div.children(".controls").children(".js-"+target).children(".icon").addClass("icon-minus");
		div.addClass(".div-"+target+"-"+i);
		div.insertBefore(".div-"+target);
		i++;
	}else{
		$(".div-"+target+"-"+id).remove();
	}
});
$(".js-itemset").on("click",function(){
	getitems("","","","order_items");
});
$(".js-cartset").on("click",function(){
	getitems("","","","cart_items");
});
var Q,PG;
var num_iids=new Array();
function getitems(q,pg,numiids,input){
	Q=q?q:"";
	PG=pg?pg:1;
	input=input?input:"order_items";
	showDialog("正在读取数据。。。","<img src=\"http://gtd.alicdn.com/tps/i3/T1b1m3XkVpXXXXXXXX-32-32.gif\" />","form-horizontal");	
	var item_iids = $('.'+input).val();
	if(item_iids!=""){
	num_iids = item_iids.split(",");
	}else{
	num_iids=new Array();
	}
	KISSY.io({
		url: "/?a=items&m=shopmanage&c=Service",
		data:{"q":Q,"pg":PG,"numiids":numiids},
		success: function(data, textStatus) {
		closeDialog();
		if(data.code){
			showDialog("读取失败！",data.msg+data.sub_msg,"form-horizontal");
			return;
		}
		if(data.total_results==0){
			showDialog("读取失败！","没有读取到任何商品哦！","form-horizontal");
		}else{
			var item="<input type=\"text\" value=\""+data.q+"\" class=\"js-gjc\" /><button type=\"button\" class=\"button button-mini js-so\">搜索</button>  共"+data.total_results+"件商品  <button type=\"button\" class=\"button button-mini button-success js-ready\" data-input=\""+input+"\">查看已设置的商品</button> <button type=\"button\" class=\"button button-mini js-ready-all\">查看全部</button> <button type=\"button\" class=\"button button-mini js-select-all\">全选本页</button>";
			item +=("<hr style=\"clear:both\">");
			for(var i=0;i<data.items.item.length;i++){
			item +=("<table style=\"width:140px;float:left;margin-right:5px\" cellspacing=\"0\" class=\"table table-bordered table-item\">");
			item +=("	<tbody>");
			item +=("	<tr><td>");
			item +=("		<a href=\"http://item.taobao.com/item.htm?id="+data.items.item[i].num_iid+"\" target=\"_blank\" ><img src=\""+data.items.item[i].pic_url+"_120x120.jpg\"></a>");
			// item +=("	</td></tr><tr><td>");
			item +=("	商品标题："+decodeURI(data.items.item[i].title));
			var css = "";
			var text = "选择";
			for (var j=0;j<num_iids.length;j++){
				if(data.items.item[i].num_iid==num_iids[j]){
					css = "button-danger";
					text = "取消选择";
					break;
				}
			}
			item +=("	<div style=\"text-align:center\"><button type=\"button\" class=\"button button-small "+css+" js-select\" data-iid=\""+data.items.item[i].num_iid+"\">"+text+"</button></div>");
			item +=("	</td></tr>");
			item +=("	</tbody>");
			item +=("</table>");
			}
			item +=("<hr style=\"clear:both\">");
			item +=("<div class=\"centered\"><button type=\"button\" class=\"button button-mini js-pgup\">上一页</button>");
			item +=("<button type=\"button\" class=\"button button-mini js-pgdown\">下一页</button></div>");
			showDialog("请选择商品",item,"form-horizontal",KISSY.DOM.scrollTop());
			$(".js-ready-all").on("click",function(){
				getitems("","","",$(this).attr("data-input"));
			});
			$(".js-select-all").on("click",function(){
				$(".js-select").fire("click");
			});
			$(".js-select").on("click",function(){
				var key = $(this).attr("data-iid");
				if($(this).hasClass("button-danger")){
					for(var i=0;i<num_iids.length;i++){
						if(num_iids[i]==key){
							num_iids.splice(i,1);
						}
					}
					$("."+input).val(num_iids.join(","));
					$(this).removeClass("button-danger");
					$(this).text("选择");
					$(".items-"+input).text(num_iids.length);
				}else{
					num_iids.push(key);
					$("."+input).val(num_iids.join(","));
					$(this).addClass("button-danger");
					$(this).text("取消选择");
					$(".items-"+input).text(num_iids.length);
				}
			});
			$(".js-pgup").on("click",function(){
				if(PG==1){
					alert("没有上一页啦！");
				}else{
					PG--;
					getitems($(".js-gjc").val(),PG,numiids,input);
				}
			});
			$(".js-pgdown").on("click",function(){
				if(PG>(data.total_results/20)){
					alert("没有下一页啦！");
				}else{
					PG++
					getitems($(".js-gjc").val(),PG,numiids,input);
				}
			});
			$(".js-ready").on("click",function(){
				var item_iids = $('.'+input).val();
				if(!item_iids){
				alert("您还没有选择任何商品！");
				}else{
				getitems("","",item_iids,input);
				}
			});
		}
		$(".js-so").on("click",function(){
			getitems($(".js-gjc").val(),"","",input);
		});
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
}
// function show_tip(select){
	// var target = $('.'+select);
	// var shell = KISSY.UA.shell;
	// var topadd=-220;
	// // switch(shell){
		// // case "firefox" :
		// // topadd=-220;
		// // break;
	// // }
	// for (var i=0;i<target.lenth;i++){
		// target.item(i).attr("data-tipc",i);
		// var top = target.item(i).offset().top+topadd;
		// var left = target.item(i).offset().left;
		// var info = target.item(i).attr("data-tip");
		// if(info=="show"){
			// var show = target.item(i).attr("data-tip-info");
			// $("."+show).addClass(select+i+'-tip');
			// KISSY.DOM.css('.'+show, "top",top+"px");
			// KISSY.DOM.css('.'+show, "left",left+"px");
		// }else{
			// var temp = '';
			// temp+='<div class="'+select+i+'-tip bui-overlay bui-ext-position tips tips-small tips-info tips-no-icon hide" style="top:'+top+'px; left:'+left+'px;">';
			// temp+='<div class="tips-content">'+info+'</div>';
			// temp+='</div>';
			// $(temp).insertAfter(target.item(i));
		// }
		// target.item(i).on("mouseenter mouseleave",function(){
			// var c = $(this).attr("data-tipc");
			// $('.'+select+(c)+"-tip").toggleClass("hide");
		// });	
	// }
// }
// show_tip("helper");
var DOM = KISSY.DOM;
var start = DOM.get('.start-calendar');
var end = DOM.get('.end-calendar');
var sign = DOM.get('.signday-calendar');
var paystart = DOM.get('.pay-start-calendar');
var payend = DOM.get('.pay-end-calendar');
var cstart = DOM.get('.c-start-calendar');
var cend = DOM.get('.c-end-calendar');
TimeSet(start,0);
TimeSet(end,2);
TimeSet(sign,0,'max');
TimeSet(paystart,-90);
TimeSet(cstart,-90);
TimeSet(payend);
TimeSet(cend,0,'max');
$(".js-useditor").on("click",function(){
	showDialog("使用高级编辑器","<div class=\"centered \">请在打开的窗口中编辑内容，完成后点击<button class=\"button read\" type=\"button\">读取</button></div>","form-horizontal");
	$(".cancel").remove();
	var content = $(".content").val();
	$(".read").on("click",function(){
		KISSY.io({
			url: "/?a=readeditor&m=shop&c=event",
			success: function(data, textStatus) {
				$(".content").val(data.content);
				closeDialog();
				showDialog("注意","已读取编辑器设置的详情代码，请不要直接修改这些代码，如需修改，请再次点击“使用高级编辑器”。","form-horizontal");
			},
			error: function(){
				alert('error');
			},
			dataType:"jsonp"
		});
	});
});
$("form").on("submit",function(){
	var start_time = $(".start-calendar").val();
	if(!CheckDate(start_time)){alert("请用日历选择开始时间！");return false;}
	var tomorrow =GetDateStr(1);
	if(!isStartEndDate(tomorrow,start_time)){
		// alert("开始日期必须在24小时之后"); return false;
	}
	var end_time = $(".end-calendar").val();
	if(!CheckDate(end_time)){alert("请用日历选择结束时间！");return false;}
	if(!isStartEndDate(start_time,end_time)){
		alert("开始日期不能大于结束日期"); return false;
	}
// alert(KISSY.DOM.get(".ks-editor").innerHTML);
	// var params = $(".form");
	// var l=(params.len());
	// var str=new Array();
	// for(var i=0;i<l;i++){
		// // if(params.item(i).attr("data-post")=="content"){
		// // if(1==1){
			// // str.push('"'+params.item(i).attr("data-post")+'":"'+params.item(i).val()+'"');
		// // }else{
			// var node = params.item(i).getDOMNode();
			// if(node.type=="checkbox"){
				// str.push('"'+params.item(i).attr("data-post")+'":"'+node.checked+'"');
			// }else{
				// var s = params.item(i).attr("data-post");alert(s);
				// if (s=="start_time"){
					// var start_time = params.item(i).val();alert(start_time);
					// if(!CheckDate(start_time)){alert("请用日历选择日期！");return false;}
					// var tomorrow =GetDateStr(1);
					// if(!isStartEndDate(start_time,tomorrow)){
						// alert("开始日期必须在24小时之后"); return false;
					// }
				// }
				// if (s=="end_time"){
					// var end_time = params.item(i).val();
					// if(!CheckDate(end_time)){alert("请用日历选择日期！");return false;}
				// }
				// if(!isStartEndDate(start_time,end_time)){
					// alert("开始日期不能大于结束日期"); return false;
				// }
				// str.push('"'+params.item(i).attr("data-post")+'":"'+params.item(i).val()+'"');
				
			// }
		// // }
	// }
	// var	tmp="<span>{"+str.join(",")+"}</span>";
	// $(tmp).appendTo(".res");
	return true;
});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
	if(m<10){m="0"+m;}
    var d = dd.getDate();
	if(d<10){d="0"+d;}
    var H = dd.getHours();
    var i = dd.getMinutes();
    var s = dd.getSeconds();
    return y+"-"+m+"-"+d+" "+H+":"+i+":"+s;
}
function CheckDate(strDate){   
	if(strDate.length>0){   
		var reg= /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;  
		return (reg.test(strDate));  
	}else{   
		return false;   
	}
}
function isStartEndDate(startTime,endTime){            
	if(startTime.length>0 && endTime.length>0){  
		var st = startTime.split(" ");  
		var et = endTime.split(" ");  
		var startTmp=st[0].split("-");  
		var endTmp=et[0].split("-");  
		var sd=new Date(startTmp[0],startTmp[1],startTmp[2]);
		var ed=new Date(endTmp[0],endTmp[1],endTmp[2]);
		if(sd.getTime()>ed.getTime()){    
			return false;     
		}     
	}     
	return true; 
}      

$(".pre_step").on("click",function(){
	location.href=("/?a=event_step1&m=shop&c=event");
});
// GS.addListener('cajaupload',function(d){
		// var response = KISSY.JSON.parse(d.content);
		// // var info = KISSY.DOM.get(".fileInfo");
		// // var img = KISSY.DOM.get('.uImage',info);
		// // var st = KISSY.DOM.get(".fileStatus", info);
		// // //var name = KISSY.DOM.get(".fileName", info);
		// // // var ID = KISSY.DOM.get(".fileId", info);
		// // // var size = KISSY.DOM.get(".fileSize", info);
		// // var url = KISSY.DOM.get(".imgurl", info);
		
		// if(response['success']){
			// // st.innerHTML = "成功";
			// // img.innerHTML = '<a href="'+response['url']+'"><img src="'+response['url']+'" width="64" /></a>';
			// // // name.innerHTML = response['name'];
			// // // ID.innerHTML = response['id'];
			// // // size.innerHTML = response['size'];
			// // url.value = response['url'];
			// $(".imgurl").val(response['url']);
			// $(".upImage").empty();
			// var str = "<img src='"+response['url']+"_120x120.jpg' />";
			// $(str).appendTo(".upImage");
		// }else{
			// st.innerHTML = "失败";
			// img.innerHTML = "";
			// // name.innerHTML = "";
			// // ID.innerHTML = "";
			// // size.innerHTML = "";
			// url.innerHTML = "";
		// }
	// });
for(var i=6;i>=0;i--){
	$(".lev"+i).on("click",function(){
		var lev = $(this).attr("data-lev");
		for(var i=6;i>=0;i--){
		KISSY.DOM.query(".lev"+i)[0].checked=false;
		}
		for(var i=6;i>=lev;i--){
			KISSY.DOM.query(".lev"+i)[0].checked=true;
		}
	});
}
var l=($(".xcount").length);
for(var i=0;i<l;i++){
$(".xcount").item(i).on("blur",function(){
	var chk = $(this).attr("data-chk");
	if($(this).val()==""||$(this).val()==0){
		KISSY.DOM.query(".chk"+chk)[0].checked=false;
	}else{
		KISSY.DOM.query(".chk"+chk)[0].checked=true;
	}
});

}
$('.areabtn').on("click",function(){
	if(KISSY.DOM.query(".areachk")[0].checked==true){
		KISSY.DOM.query(".areachk")[0].checked=false;
		KISSY.DOM.css('.arease', "visibility","hidden");
		KISSY.DOM.css('.arease', "height","0");
	}else{
		KISSY.DOM.query(".areachk")[0].checked=true;
		KISSY.DOM.css('.arease', "visibility","visible");
		KISSY.DOM.css('.arease', "height","auto");
	}
});

$('.blselect').on("change",function(){
	$(".buyerlev").empty();
	if(this.value>4){
		var str = '<img src="'+GetCreditString(this.value)+'" />';
		// $(".buyerlev").innerHtml=(str);
		$(str).appendTo(".buyerlev");
	}
});
$(".aid0").on("click",function(){
	$('.bui-list-item').removeClass('bui-list-item-selected');
});
$('.bui-list-item').on("click",function(){
	$(".aid0").removeClass('bui-list-item-selected');
	$(this).toggleClass('bui-list-item-selected');
	var areas = new Array();
	var selected = $('.bui-list-item-selected');
	for(var i=0;i<selected.len();i++){
		areas.push(selected.item(i).attr('data-areaid'));
	}
	$('.areainput').val(areas.join(","));
	if(!$(".bui-list-item").hasClass('bui-list-item-selected')){
		$(".aid0").addClass('bui-list-item-selected');
	}
});
function GetCreditString($lev){
		if ($lev>4&&$lev<=10){
		$lev='http://pics.taobaocdn.com/newrank/b_red_1.gif';
		}else if($lev>10&&$lev<=40){
		$lev='http://pics.taobaocdn.com/newrank/b_red_2.gif';
		}else if($lev>40&&$lev<=90){
		$lev='http://pics.taobaocdn.com/newrank/b_red_3.gif';
		}else if($lev>90&&$lev<=150){
		$lev='http://pics.taobaocdn.com/newrank/b_red_4.gif';
		}else if($lev>150&&$lev<=250){
		$lev='http://pics.taobaocdn.com/newrank/b_red_5.gif';
		}else if($lev>250&&$lev<=500){
		$lev='http://pics.taobaocdn.com/newrank/b_blue_1.gif';
		}else if($lev>500&&$lev<=1000){
		$lev='http://pics.taobaocdn.com/newrank/b_blue_2.gif';
		}else if($lev>1000&&$lev<=2000){
		$lev='http://pics.taobaocdn.com/newrank/b_blue_3.gif';
		}else if($lev>2000&&$lev<=5000){
		$lev='http://pics.taobaocdn.com/newrank/b_blue_4.gif';
		}else if($lev>5000&&$lev<=10000){
		$lev='http://pics.taobaocdn.com/newrank/b_blue_5.gif';
		}else if($lev>10000&&$lev<=20000){
		$lev='http://pics.taobaocdn.com/newrank/b_cap_1.gif';
		}else if($lev>20000&&$lev<=50000){
		$lev='http://pics.taobaocdn.com/newrank/b_cap_2.gif';
		}else if($lev>50000&&$lev<=100000){
		$lev='http://pics.taobaocdn.com/newrank/b_cap_3.gif';
		}else if($lev>100000&&$lev<=200000){
		$lev='http://pics.taobaocdn.com/newrank/b_cap_4.gif';
		}else if($lev>200000&&$lev<=500000){
		$lev='http://pics.taobaocdn.com/newrank/b_cap_5.gif';
		}else if($lev>500000&&$lev<=1000000){
		$lev='http://pics.taobaocdn.com/newrank/b_crown_1.gif';
		}else if($lev>1000000&&$lev<=2000000){
		$lev='http://pics.taobaocdn.com/newrank/b_crown_2.gif';
		}else if($lev>2000000&&$lev<=5000000){
		$lev='http://pics.taobaocdn.com/newrank/b_crown_3.gif';
		}else if($lev>5000000&&$lev<=10000000){
		$lev='http://pics.taobaocdn.com/newrank/b_crown_4.gif';
		}else if($lev>10000000){
		$lev='http://pics.taobaocdn.com/newrank/b_crown_5.gif';
		}
	return $lev;
}
function TimeSet(input,addday,limit){
	KISSY.use('bui/calendar',function(S,Calendar){
    // use Calendar
		var datepicker = new Calendar.DatePicker({
            trigger:input,
			showTime : true,
            autoRender : true
        })

		// // var Calendar = KISSY.Calendar;
		// // var date = new Date();
		// // var param;
		// // if(limit=='max'){
		// // param = {popup: true, maxDate: date.setDate(date.getDate()+addday),showTime:true};
		// // }else{
		// // param = {popup: true, minDate: date.setDate(date.getDate()+addday),showTime:true};
		// // }
		// // var cal = new Calendar(input,param);
		// // cal.on('timeSelect', function (e) {
			// // var selectDate = new Date(e.date);
			// // var m = selectDate.getMonth()+1;
			// // var d = selectDate.getDate();
			// // var h = selectDate.getHours();
			// // var i = selectDate.getMinutes();
			// // var s = selectDate.getSeconds();
			// // m = m<10?'0'+m:m;
			// // d = d<10?'0'+d:d;
			// // h = h<10?'0'+h:h;
			// // i = i<10?'0'+i:i;
			// // s = s<10?'0'+s:s;
			
			// // var t = selectDate.getFullYear() + "-" + m + "-" + d +" "+h+":"+i+":"+s;
			// // input.value = t;
			// // cal.hide();
		// // });
	});
}

$(".repick").on("click",function(){
	location.href=("/?a=gamecfg&m=shopmanage&repick=1");
});
});